//package com.lrj.mysecurity.security;
//
//import com.lrj.mysecurity.entity.User;
//import com.lrj.mysecurity.service.UserService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.crypto.factory.PasswordEncoderFactories;
//import org.springframework.security.crypto.password.PasswordEncoder;
//
////@EnableWebSecurity已经包含了@Configuration，可以不用加@Configuration
////@Configuration
//@EnableWebSecurity
//public class WebSecurityConfig {
///**
// * 配置内存用户信息
// * 原理其实是现在保存用户信息在内存中
// * 等用户登录时，对比账号和密码
// * 不管是用什么方法，在Spring Security中
// * 都会执行UserDetailsService的接口loadUserByUsername
// * 并且返回一个UserDetails接口实现类
// */
////@EnableWebSecurity
//public class MemoryWebSecurityConfig {
//
//    @Autowired
//    private UserService userService;
//
////    @Bean
////    public UserDetailsService userDetailsService() {
////        //创建密码编码器
////        PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
////        //创建内存UserDetails管理器（实现了UserDetailsService接口）
////        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
////        //添加用户
////        manager.createUser(
////                //这里的User是org.springframework.security.core.userdetails.User
////                User.withUsername("user1")
////                        .password("123")
////                        .passwordEncoder(encoder::encode)
////                        //如果没有设置role，会报异常（Cannot pass a null GrantedAuthority collection）
////                        .roles("")
////                        .build()
////        );
////
////        manager.createUser(
////                User.withUsername("user2")
////                        .password("456")
////                        .passwordEncoder(encoder::encode)
////                        .roles("")
////                        .build()
////        );
////
////
////        return manager;
////    }
//
////    @Bean
////    public UserDetailsService userDetailsService() {
//////        return new UserDetailsService() {
//////            @Override
//////            public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
//////                return null;
//////            }
//////        };
////
////        //这种写法和上面是一样的效果
////        return (userName) -> {
////            User user = userService.getByName(userName);
////            if (user == null) {
////                throw new RuntimeException("用户不存在");
////            }
////            PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
////            String encodedPassword = encoder.encode(user.getPassword());
////
////            return org.springframework.security.core.userdetails.User.withUsername(user.getName())
////                    .password(encodedPassword)
////                    .roles("")
////                    .build();
////        };
////    }
//}
